package main

import (
	"fmt"
	"github.com/iris-contrib/middleware/cors"
	"github.com/kataras/iris/v12"
	"log"
	"main/database"
	"main/env"
	"main/router"
)

func main() {
	log.SetPrefix(red("[LOG] "))

	log.SetFlags(log.Lshortfile)

	log.Println("环境启动")
	// 初始化env
	env.Init()

	ormdb, err := database.InitGorm()
	if err != nil {
		log.Println(err)
		panic(err)
	}

	log.SetFlags(log.Lshortfile)

	err = database.InitMongoDB()
	if err != nil {
		log.Println(err)
		panic(err)
	}

	// 创建iris主程序
	app := iris.New()

	app.RegisterView(iris.HTML("./dist", ".html"))
	app.Get("/", func(ctx iris.Context) {
		ctx.View("index.html")
	})

	app.HandleDir("/static", "./dist/static")

	crs := cors.AllowAll()

	storeAPI := app.Party("/api", crs)

	router.SetRouter(ormdb, storeAPI)

	_ = app.Run(iris.Addr(fmt.Sprintf(":%s", env.Port)), iris.WithOptimizations, iris.WithPostMaxMemory(32<<22)) // 128M
}

const colorRed = uint8(iota + 91)

func red(s string) string {
	return fmt.Sprintf("\x1b[%dm%s\x1b[0m", colorRed, s)
}
